package com.typ.member.web.api;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.typ.member.common.model.PageRequest;
import com.typ.member.common.model.Response;
import com.typ.member.common.model.ResponseUtil;
import com.typ.member.dao.model.MemberCardDto;
import com.typ.member.dao.model.MemberCardQueryDto;
import com.typ.member.dao.model.ProcessDto;
import com.typ.member.service.MemberCardService;
import com.typ.member.web.model.MemberCardQueryVo;
import com.typ.member.web.model.MemberCardVo;
import com.typ.member.web.model.ProcessVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员卡信息API
 *
 * @author heran
 * @version 1.0, 2019-05-09 18:09
 */
@RestController
@Slf4j
@RequestMapping("/api/member/card")
public class MemberCardApiController {

    @Resource
    private MemberCardService memberCardService;

    @SuppressWarnings("Duplicates")
    @GetMapping("/search")
    public Response search(@ModelAttribute MemberCardQueryVo memberCardQueryVo, @ModelAttribute PageRequest pageRequest) {
        MemberCardQueryDto memberCardQueryDto = translateMemberCardQueryVo(memberCardQueryVo);
        PageInfo<MemberCardDto> pageInfo = memberCardService.search(memberCardQueryDto, pageRequest);
        PageInfo<MemberCardVo> result = new PageInfo<>();
        BeanUtils.copyProperties(pageInfo, result);
        result.setList(convert2MemberCardVoList(pageInfo.getList()));
        return ResponseUtil.createSuccess(result);
    }

    @PostMapping("/add")
    public Response add(@RequestBody MemberCardVo memberCardVo) {
        MemberCardDto memberCardDto = new MemberCardDto();
        BeanUtils.copyProperties(memberCardVo, memberCardDto);
        try {
            memberCardService.add(memberCardDto);
            return ResponseUtil.createSuccess();
        } catch (Exception e) {
            log.error("add occur error,memberCardVo={}", memberCardVo, e);
            return ResponseUtil.createError(e.getMessage());
        }
    }


    @PostMapping("/update")
    public Response update(@RequestBody MemberCardVo memberCardVo) {
        MemberCardDto memberCardDto = new MemberCardDto();
        BeanUtils.copyProperties(memberCardVo, memberCardDto);
        try {
            memberCardService.update(memberCardDto);
            return ResponseUtil.createSuccess();
        } catch (Exception e) {
            log.error("update occur error,memberCardVo={}", memberCardVo, e);
            return ResponseUtil.createError(e.getMessage());
        }
    }

    @SuppressWarnings("Duplicates")
    @PostMapping("/delete")
    public Response delete(@RequestBody String param) {
        Long id = JSON.parseObject(param).getLong("id");
        try {
            memberCardService.delete(id);
            return ResponseUtil.createSuccess();
        } catch (Exception e) {
            log.error("delete occur error,id={}", id, e);
            return ResponseUtil.createError(e.getMessage());
        }
    }

    /**
     * 余额消费
     *
     * @param processVo
     */
    @PostMapping("/doConsume")
    public Response doConsume(@RequestBody ProcessVo processVo) {
        try {
            ProcessDto processDto = new ProcessDto();
            BeanUtils.copyProperties(processVo, processDto);
            memberCardService.doConsume(processDto);
            return ResponseUtil.createSuccess();
        } catch (Exception e) {
            log.error("doConsume occur error,processVo={}", processVo, e);
            return ResponseUtil.createError(e.getMessage());
        }
    }

    /**
     * 余额充值
     *
     * @param processVo
     */
    @PostMapping("/doRecharge")
    public Response doRecharge(@RequestBody ProcessVo processVo) {
        try {
            ProcessDto processDto = new ProcessDto();
            BeanUtils.copyProperties(processVo, processDto);
            memberCardService.doRecharge(processDto);
            return ResponseUtil.createSuccess();
        } catch (Exception e) {
            log.error("doRecharge occur error,processVo={}", processVo, e);
            return ResponseUtil.createError(e.getMessage());
        }
    }

    /**
     * 积分消费
     *
     * @param processVo
     */
    @PostMapping("/doPointsConsume")
    public Response doPointsConsume(@RequestBody ProcessVo processVo) {
        try {
            ProcessDto processDto = new ProcessDto();
            BeanUtils.copyProperties(processVo, processDto);
            memberCardService.doPointsConsume(processDto);
            return ResponseUtil.createSuccess();
        } catch (Exception e) {
            log.error("doPointsConsume occur error,processVo={}", processVo, e);
            return ResponseUtil.createError(e.getMessage());
        }
    }

    private List<MemberCardVo> convert2MemberCardVoList(List<MemberCardDto> memberCardDtos) {
        if (CollectionUtils.isEmpty(memberCardDtos)) {
            return Lists.newArrayList();
        }
        return memberCardDtos.stream().map(memberCardDto -> {
            MemberCardVo memberCardVo = new MemberCardVo();
            BeanUtils.copyProperties(memberCardDto, memberCardVo);
            return memberCardVo;
        }).collect(Collectors.toList());
    }

    public MemberCardQueryDto translateMemberCardQueryVo(MemberCardQueryVo input) {
        if (input == null) {
            return null;
        }
        MemberCardQueryDto result = new MemberCardQueryDto();
        result.setId(input.getId());
        result.setName(input.getName());
        result.setPhone(input.getPhone());
        if (input.getCreatedStart() != null) {
            result.setCreatedStart(new Date(input.getCreatedStart()));
        }
        if (input.getCreatedEnd() != null) {
            result.setCreatedEnd(new Date(input.getCreatedEnd()));
        }
        if (input.getModifiedStart() != null) {
            result.setModifiedStart(new Date(input.getModifiedStart()));
        }
        if (input.getModifiedEnd() != null) {
            result.setModifiedEnd(new Date(input.getModifiedEnd()));
        }
        return result;
    }

}
